<template>
  <div>
    <!--新增或者编辑的弹出框-->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="40%">
      <el-form class="form" :model="modelForm" :rules="rules" ref="modelForm" style="text-align: center">
        <el-form-item label="餐品名称" :label-width="formLabelWidth" align="left" prop="mname">
          <el-input v-model="modelForm.mname" autocomplete="off" :style="style"></el-input>
        </el-form-item>
        <el-form-item label="餐品价格" :label-width="formLabelWidth" align="left" prop="price">
          <el-input v-model="modelForm.price" autocomplete="off" :style="style"></el-input>
        </el-form-item>
        <el-form-item label="餐品图片" :label-width="formLabelWidth" align="left" prop="picture">
          <el-upload
            ref="upload"
            :class="{disabled:uploadDisabled}"
            action="api/upload"
            :headers="headers"
            list-type="picture-card"
            :limit="1"
            :auto-upload="false"
            :on-change="handleChange"
            :on-success="handleSuccess"
            :on-error="handleError"
            :file-list="fileList"
          >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url" alt=""
              >
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="upload">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
    import modelMixin from '@/mixins/modelMixin'
    import imageUploadMixin from '@/mixins/imageUploadMixin'

    export default {
        name: "MealModel",
        mixins: [modelMixin,imageUploadMixin],
        data() {
            return {
                //表单标题
                title: "餐品信息",
                //表单数据
                modelForm: {
                    mealno: null,
                    mname: null,
                    price: null,
                    picture: null
                },
                //验证规则
                rules: {
                    //餐品名称不能为空
                    mname: [
                        {required: true, message: '请输入餐品名称', trigger: 'blur'}
                    ],
                    //餐品价格不能为空
                    price: [
                        {required: true, message: '请输入餐品价格', trigger: 'blur'}
                    ],
                },
                //后台接口路径
                url: {
                    addData: 'addMeal',
                    editData: 'editMeal'
                },
                //主键
                key: 'mealno'
            }
        },
        methods: {
            /**
             * 重置表单
             */
            resetModelForm() {
                this.fileList = []
                this.modelForm = {
                    mealno: null,
                    mname: null,
                    price: null,
                    picture: ""
                }
            },
            /**
             * 上传图片成功回调
             * @param res
             */
            handleSuccess(res) {
                this.modelForm.picture = res.data.newFileName
                this.addOrEditData()
            }
        }
    }
</script>

<style scoped>

</style>